package algorithms;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.ObjectInputStream;

import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.Classifier;
import weka.core.*;

public class BK {

	public static Classifier getInstance() throws Exception {
		
		NaiveBayes bayes = new NaiveBayes();
		bayes.setUseKernelEstimator(true);
		
		return bayes;
	}
	
   public static Classifier buildModelTo(String data, String model) throws Exception {
		
	   weka.classifiers.bayes.NaiveBayes bayes = new weka.classifiers.bayes.NaiveBayes();
		bayes.setUseKernelEstimator(true);
		Classifier cls = (Classifier) bayes;
		
		BufferedReader reader = new BufferedReader(
                new FileReader(data));
		
		Instances instances = new Instances(reader);
		instances.setClassIndex(instances.numAttributes() - 1);
		cls.buildClassifier(instances);
		
		weka.core.SerializationHelper.write(model, cls);
		
		return bayes;
	}
	
	public static Classifier loadModel(String model) throws Exception {
		
		ObjectInputStream ois = new ObjectInputStream(
				new FileInputStream(model));
		NaiveBayes bayes = null;
		Classifier cls = (Classifier) weka.core.SerializationHelper.read(model);
		ois.close();
		bayes = (NaiveBayes) cls;
		bayes.setUseKernelEstimator(true);
		return bayes;
	}
	
}
